package com.douyu.water.core.web.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.douyu.water.core.DwzResult;
import com.douyu.water.core.PagePara;
import com.douyu.water.core.model.Area;
import com.douyu.water.core.model.Role;
import com.douyu.water.core.model.UserArea;
import com.douyu.water.core.service.AreaService;

@Controller
@RequestMapping("/area/*")
public class AreaController {

	@Resource
	private AreaService areaService;
	
	/**
	 * 检索地区列表
	 * @param request
	 * @param area Area
	 * @param page PagePara
	 * @return String
	 */
	@RequestMapping("/select")
	public String select(HttpServletRequest request,Area area,PagePara page){
		String result ="WEB-INF/jsp/area/list";
		try
		{
			List<Area> list = areaService.select(area, page);
			request.setAttribute("list",list);
			request.setAttribute("area", area);
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}
	
	@RequestMapping("/selectAuth")
	public String selectAuth(HttpServletRequest request,Area area)
	{
		String result = "WEB-INF/jsp/area/auth";
		try
		{
			List<Area> list = areaService.selectAuth(area);
			request.setAttribute("list", list);
			request.setAttribute("area", area);
		}
		catch(Exception e)
		{
			e.printStackTrace();			
		}
		return result;
	}
	/**
	 * 新建页面
	 * @param request
	 * @param area
	 * @return
	 */
	@RequestMapping("/add")
	public String add(HttpServletRequest request,Area area)
	{
		String result = "WEB-INF/jsp/area/add";
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult",dwzResult);
		try
		{
			List<Area> list = areaService.selectParent(area);
			request.setAttribute("parent",list);
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 新建
	 * @param request
	 * @param area Area
	 * @return
	 */
	@RequestMapping("/insert")
	public String insert(HttpServletRequest request,Area area)
	{
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult",dwzResult);
		try
		{
			int flag = areaService.insert(area);
			switch(flag)
			{
			case 0:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("添加失败");
				break;
			case 1:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_SUCCESS);
				dwzResult.setMessage("添加成功");
				dwzResult.setNavTabId("areaList");
				dwzResult.setCloseCurrent(true);
				break;
			case 2:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("名称重复，添加失败");
				break;
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return DwzResult.URL;
	}
	/**
	 * 修改页面
	 * @param request
	 * @param record Area
	 * @return
	 */
	@RequestMapping("/edit")
	public String edit(HttpServletRequest request,Area record)
	{
		String result ="WEB-INF/jsp/area/edit";
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		try{
			Area area = areaService.view(record);
			if(area != null)
			{
				request.setAttribute("edt_area", area);
			}
			else
			{
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("未能获取对象");
				return DwzResult.URL;
			}
		}
		catch(Exception e){
			e.printStackTrace();
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
			dwzResult.setMessage("系统错误:" + e.getMessage());
			return DwzResult.URL;
		}
		return result;
	}
	
	/**
	 * 修改
	 * @param request
	 * @param area Area  
	 *        -行政区划
	 * @return String
	 */
	@RequestMapping("/update")
	public String update(HttpServletRequest request,Area area)
	{
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		try
		{
			int flag = areaService.update(area);
			switch(flag)
			{
			case 0:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("更新失败");
				break;
			case 1:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_SUCCESS);
				dwzResult.setMessage("更新成功");
				dwzResult.setCloseCurrent(true);
				dwzResult.setNavTabId("areaList");
				break;
			case 2:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("名称重复，更新失败");
				break;
			}
		}
		catch(Exception e){
			e.printStackTrace();
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
			dwzResult.setMessage("系统错误:" + e.getMessage());
		}
		return DwzResult.URL;
	}
	
	/**
	 * 删除
	 * @param request
	 * @param area Area   
	 *        -编号
	 * @return  String
	 */
	@RequestMapping("/delete")
	public String delete(HttpServletRequest request,Area area)
	{
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		try
		{
			int flag = areaService.delete(area);
			switch(flag)
			{
			case 0:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("删除失败");
				break;
			case 1:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_SUCCESS);
				dwzResult.setMessage("删除成功");
				dwzResult.setCloseCurrent(false);
				dwzResult.setNavTabId("areaList");
				break;
			case 2:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("正在使用，无法删除");
				break;
			}
		}
		catch(Exception e){
			e.printStackTrace();
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
			dwzResult.setMessage("系统错误:" + e.getMessage());
		}
		return DwzResult.URL;
	}
	
	@RequestMapping("/insertAuth")
	public String insertAuth(HttpServletRequest request,UserArea ua)
	{
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		try
		{
			int flag = areaService.insertAuth(ua);
			if(flag > 0)
			{
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_SUCCESS);
				dwzResult.setMessage("分配成功");
				dwzResult.setCloseCurrent(true);
				dwzResult.setNavTabId("userList");
			}
			else
			{
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("分配失败");
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
			dwzResult.setMessage("系统错误:" + e.getMessage());
		}
		return DwzResult.URL;
	}
}
